package xipujewel.xipujewel.web.account;

import java.util.List;

import javax.validation.Valid;

import net.esoar.modules.web.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import xipujewel.xipujewel.entity.app.ExchangeRate;
import xipujewel.xipujewel.service.ExchangeRateManager;

/**
 * 
 * @author hang
 *
 */
@Controller
@RequestMapping(value = "/exchangeRate")
public class ExchangeRateController extends BaseController<ExchangeRate> {

	@Autowired
	private ExchangeRateManager exchangeRateManager;

	@RequestMapping("")
	public String getRate(Model model) {
		ExchangeRate exchangeRate = new ExchangeRate();
		List<ExchangeRate> exchangeRates = exchangeRateManager.getAll();
		if (exchangeRates.size() == 0) {
			exchangeRateManager.save(exchangeRate);
		} else {
			exchangeRate = exchangeRates.get(0);
		}
		model.addAttribute("exchangeRate", exchangeRate);
		return "exchangeRate/exchangeRate";
	}

	@RequestMapping(value = SAVE)
	public String save(@Valid @ModelAttribute("exchangeRate") ExchangeRate exchangeRate) {
		exchangeRateManager.save(exchangeRate);
		return "redirect:/exchangeRate";
	}

	@Override
	public String baseMapping() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getModelName() {
		// TODO Auto-generated method stub
		return null;
	}

	@ModelAttribute("exchangeRate")
	public ExchangeRate getModel(@RequestParam(value = ID, required = false) Long id) {
		if (id != null) {
			return exchangeRateManager.get(id);
		}
		return null;
	}

}
